/*
 * Copyright (C) 2011 Dirk Weißenborn, dirk.weissenborn@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.weissenborn.colabtex.client.activities;

import com.weissenborn.colabtex.client.ClientFactory;
import com.weissenborn.colabtex.client.events.LogOutEvent;
import com.weissenborn.colabtex.client.places.Dashboard;
import com.weissenborn.colabtex.client.places.Login;
import com.weissenborn.colabtex.client.service.UserManagementService;
import com.weissenborn.colabtex.client.service.UserManagementServiceAsync;
import com.weissenborn.colabtex.client.view.AccountManagerView;
import com.weissenborn.colabtex.client.view.AccountManagerViewImpl;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

/**
 * The Class AccountManagerActivity which controls the view of the account management
 */
public class AccountManagerActivity extends AbstractActivity implements AccountManagerView.Presenter {
	
	/** The client factory. */
	private ClientFactory clientFactory;
	
	/**
	 * Instantiates a new account manager activity.
	 *
	 * @param clientFactory the client factory
	 */
	public AccountManagerActivity(ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
	}

	/* (non-Javadoc)
	 * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget, com.google.gwt.event.shared.EventBus)
	 */
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		AccountManagerView view = new AccountManagerViewImpl();
		view.setPresenter(this);
		containerWidget.setWidget(view.asWidget());
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.AccountManagerView.Presenter#goTo(com.google.gwt.place.shared.Place)
	 */
	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.AccountManagerView.Presenter#changeAccount(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	public void changeAccount(final String newMail, String newPassword, String repeatPassword, final String currentPassword) {
		if(!newMail.isEmpty()) {
			changeUsername(newMail, currentPassword);
			return;
		}
		
		if(currentPassword.length()>3)
			if(newPassword.length()>3) {
				UserManagementServiceAsync service = UserManagementService.Util.getInstance();
				
				AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
	
					public void onFailure(Throwable caught) {
						Window.alert("No connection to server!");
					}
	
					public void onSuccess(Boolean result) {
						if(result) {
							Window.alert("Password was set to new password!");
						}
						else {	
							Window.alert("Couldn't set your password!");
						}
					}
				};
				
				service.setNewPassword(clientFactory.getUser().getId(), currentPassword, newPassword, callback);
				return;
			}else {				
				if(!newPassword.isEmpty())
					Window.alert("New password to short!");				
			}
		else				
			Window.alert("Password missing!");
		
	}

	/**
	 * Change userId to a new mail address
	 *
	 * @param newMail the new mail address
	 * @param currentPassword the current password used
	 */
	private void changeUsername(String newMail, String currentPassword) {
		if(!currentPassword.isEmpty())
			if(!newMail.isEmpty()&&newMail.contains("@")&&Window.confirm("Sure you want to change your username (email) to: "+newMail+" ?\n" +
					"If so click OK and you will receive a new password sent to your new adress!\n" +
					"You have one hour for your first login, otherwise your account will be deleted!")) {
				UserManagementServiceAsync service = UserManagementService.Util.getInstance();
				
				AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
	
					public void onFailure(Throwable caught) {
						Window.alert("No connection to server!");
					}
	
					public void onSuccess(Boolean result) {
						if(result) {
							clientFactory.getEventBus().fireEvent(new LogOutEvent());
							goTo(new Login(new Dashboard()));
						}
						else {	
							Window.alert("Couldn't reset your username!");
						}
					}
				};
				
				service.setNewEmail(clientFactory.getUser().getId(), newMail, currentPassword, callback);			
			}else;
		else
			Window.alert("Password required!");
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.AccountManagerView.Presenter#deleteAccount(java.lang.String)
	 */
	public void deleteAccount(String currentPassword) {
		if(!currentPassword.isEmpty())
			if(Window.confirm("Sure you want to delete your account?")) {
				UserManagementServiceAsync service = UserManagementService.Util.getInstance();
				
				AsyncCallback<Void> callback = new AsyncCallback<Void>() {
	
					public void onFailure(Throwable caught) {
						Window.alert("No connection to server!");
					}
	
					public void onSuccess(Void result) {
						clientFactory.getEventBus().fireEvent(new LogOutEvent());
						goTo(new Login(new Dashboard()));
					}
				};
				
				service.deleteUser(clientFactory.getUser().getId(), currentPassword, callback);			
			}else;
		else
			Window.alert("Password required!");
	}
}
